package com.awenhui.demo.module.samsql.service.impl;

import com.awenhui.demo.module.samsql.dao.SamSqlMapper;
import com.awenhui.demo.module.samsql.dao.SamSqlParamInMapper;
import com.awenhui.demo.module.samsql.model.entity.SamSqlEntity;
import com.awenhui.demo.module.samsql.model.entity.SamSqlParamInEntity;
import com.awenhui.demo.module.samsql.service.SamsqlBaseService;
import com.awenhui.demo.module.samsql.service.SamsqlDemoService;
import com.awenhui.demo.module.samsql.utils.MyBatisSqlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author yuxf
 * @version 1.0
 * @date 2021/4/23 9:44
 */
@Service
public class SamsqlDemoServiceImpl extends SamsqlBaseService  implements SamsqlDemoService {


    /**
     * 查询单个结果
     *
     * @param request
     * @return
     */
    public Object getObject(HttpServletRequest request) {
        String sql = getSql(request);
        LinkedHashMap<String, Object> sqlResult = samSqlMapper.findSqlResult(sql);
        return sqlResult;
    }

    /**
     * 查询列表
     *
     * @param request
     * @return
     */
    public Object getList(HttpServletRequest request) {
        String sql = getSql(request);
        List<LinkedHashMap<String, Object>> sqlResult = samSqlMapper.getSqlResult(sql);
        return sqlResult;
    }

    /**
     * 获取要执行的sql语句
     *
     * @param request
     * @return
     */
    private String getSql(HttpServletRequest request) {
        SamSqlEntity samSql = samSqlMapper.getSqlByCode(request.getParameter("sqlCode"));
        List<SamSqlParamInEntity> samSqlParamInList = samSqlParamInMapper.findSamSqlParamInList(samSql.getId());
        String sql = MyBatisSqlUtils.getSql(request, samSql.getSqlStr(), samSqlParamInList);
        return sql;
    }
}
